package com.shwread.android.ui.dialog;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.shwread.android.qysw10000136.R;
import com.shwread.android.ui.widget.datepicker.AMPMWheelAdapter;
import com.shwread.android.ui.widget.datepicker.NumericWheelAdapter;
import com.shwread.android.ui.widget.datepicker.OnWheelChangedListener;
import com.shwread.android.ui.widget.datepicker.WheelView;
import com.shwread.httpsdk.util.Util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import logic.util.DateUtil;

/**
 * 作者: 司海林
 * 时间: 16/1/26
 * 邮箱: 371184359@qq.com
 * 描述:
 */
public class LeaveDateDialog extends
        Dialog
{
    private Context activity;
    private String value;
    private DatePickerListener listener;
    private int minValue = 2000;
    private int maxValue = 2050;
    private TextView cancel,ok;
    private WheelView yearPicker,monthPicker,dayPicker,amPicker;

    public LeaveDateDialog(Context activity, String value, DatePickerListener listener) {
        super(activity, R.style.common_dialog);
        this.activity = activity;
        this.value=value;
        this.listener = listener;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
        initView();
        initData();
        getWindow().setLayout(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
    }

    private void initView() {
        final View view = LayoutInflater.from(activity).inflate(R.layout.leave_date_dialog, null);
        yearPicker = (WheelView) view.findViewById(R.id.datetime_picker_year);
        monthPicker = (WheelView) view.findViewById(R.id.datetime_picker_month);
        dayPicker = (WheelView) view.findViewById(R.id.datetime_picker_day);
        amPicker = (WheelView) view.findViewById(R.id.datetime_picker_am);
        cancel = (TextView) view.findViewById(R.id.datetime_cancel);
        ok = (TextView) view.findViewById(R.id.datetime_ok);
        setContentView(view);

        ClickListener l = new ClickListener();
        ok.setOnClickListener(l);
        cancel.setOnClickListener(l);
    }

    private class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.datetime_ok:
                    clickOk();
                    break;
                case R.id.datetime_cancel:
                    clickCancel();
                    break;
            }
        }
    }
    private void clickCancel() {
        dismiss();
    }
    private void clickOk() {
        listener.onSelect(getValue());
//        activity.getUIHandler().sendEmptyMessage(DefaultConsts.PERSONAL_UPDATEUI_USERINFO_BIRTHDAY);
        dismiss();
    }
    private String getValue(){
        StringBuffer sb = new StringBuffer();
        sb.append(yearPicker.getCurrentItem()+minValue);
        sb.append("年");
        sb.append(format(monthPicker.getCurrentItem()+1));
        sb.append("月");
        sb.append(format(dayPicker.getCurrentItem()+1));
        sb.append("日");
        if(amPicker.getCurrentItem() ==0)
            sb.append("上午");
        else
            sb.append("下午");
        System.out.println("DatePickerDialog --> "+sb.toString());
        return sb.toString();
    }
    private String format(int value){
        return String.format("%02d", value);
    }
    private void initData() {
        int curYear = 0;
        int curMonth = 0;
        int curDay = 0;
        if(Util.isNotEmpty(value)) {
            try {
                String dateStr = null;
                Date date = null;
                // 创建 Calendar 对象
                Calendar calendar = Calendar.getInstance();
            if(value.contains("上午")){
                dateStr =  value.substring(0, value.indexOf("上"));
                DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
                date = df.parse(dateStr);

                calendar.setTime(date);
                curYear = calendar.get(Calendar.YEAR);
                curMonth = calendar.get(Calendar.MONTH) + 1;
                curDay = calendar.get(Calendar.DAY_OF_MONTH);

            }else if(value.contains("下午")){
                dateStr =  value.substring(0, value.indexOf("下"));
                DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
                date = df.parse(dateStr);

                calendar.setTime(date);
                curYear = calendar.get(Calendar.YEAR);
                curMonth = calendar.get(Calendar.MONTH) + 1;
                curDay = calendar.get(Calendar.DAY_OF_MONTH);

            }else{
                curYear = DateUtil.getYear();
                curMonth = DateUtil.getMonth();
                curDay = DateUtil.getDay();
            }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            curYear = DateUtil.getYear();
            curMonth = DateUtil.getMonth();
            curDay = DateUtil.getDay();
        }
        System.out.println("curYear:"+curYear);
        System.out.println("curMonth:"+curMonth);
        System.out.println("curDay:"+curDay);

        yearPicker.setAdapter(new NumericWheelAdapter(minValue, maxValue));
        yearPicker.setLabel("年");
        yearPicker.setCyclic(true);
        yearPicker.setCurrentItem(curYear - minValue);

        monthPicker.setAdapter(new NumericWheelAdapter(1, 12,"%02d"));
        monthPicker.setLabel("月");
        monthPicker.setCyclic(true);
        monthPicker.setCurrentItem(curMonth-1);

        dayPicker.setAdapter(new NumericWheelAdapter(1, DateUtil.getMaxDayByMonth(curYear, curMonth-1),"%02d"));
        dayPicker.setLabel("日");
        dayPicker.setCyclic(true);
        dayPicker.setCurrentItem(curDay-1);

        amPicker.setAdapter(new AMPMWheelAdapter());
        amPicker.setLabel("午");
        amPicker.setCyclic(false);
        amPicker.setCurrentItem(0);

        OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                int v = DateUtil.getMaxDayByMonth(yearPicker.getCurrentItem(), monthPicker.getCurrentItem());
                dayPicker.setAdapter(new NumericWheelAdapter(1, v,"%02d"));
                dayPicker.setLabel("日");
                dayPicker.setCurrentItem(0);
            }
        };
        yearPicker.addChangingListener(wheelListener);
        monthPicker.addChangingListener(wheelListener);

    }
}
